<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * @package	
 */
class HelpDeskViewTools extends HelpDeskView 
{
	/**
	 * (non-PHPdoc)
	 * @see v1_00/com_helpdesk/site/HelpDeskView#display($tpl)
	 */
	function display($tpl=null) 
	{
		$layout = $this->getLayout();
		switch(strtolower($layout))
		{
			case "view":
			case "form":
				$this->_form($tpl);
			  break;
			case "default":
			default:
				$this->_default($tpl);
			  break;
		}
	}

	/**
	 * 
	 * @param $tpl
	 * @return unknown_type
	 */
	function _default($tpl=null) 
	{		
		$mainframe	= HelpDeskConfig::getApplication();
		$user 		= HelpDeskConfig::getUser();
		$database 	= HelpDeskConfig::getDBO();
		$model 		= $this->getModel();
		$app 		= HelpDeskConfig::getApplication();

		// check config		
			$config = &HelpDeskConfig::getInstance();
			$this->assignRef( 'config', $config );			
						
        // Get data from the model
        	$filtered = array();
	        $items = $model->getData();
			for ($i=0; $i<count($items); $i++) 
			{
				$item = &$items[$i];
				// Check if they have an event
				if ($hasEvent = HelpDeskHelperTools::hasEvent( $item, 'onListToolsHelpDesk' )) {
					// add item to filtered array
					$filtered[] = $item;
				}
			}
			$items = $filtered;
			$this->assignRef( 'items', $items );

		// filterlists
			$javascript = 'onchange="document.adminForm.submit();"';
			$filterLists = array();
				$types = array();
				$types[] = HelpDeskHTML::selectOption( '', '-'.HelpDeskText::_( 'Select State' ).'-' );
				$types[] = HelpDeskHTML::selectOption( '0', HelpDeskText::_( 'Unpublished' ) );
				$types[] = HelpDeskHTML::selectOption( '1', HelpDeskText::_( 'Published' ) );
			$filterLists[] = HelpDeskHTML::genericList( $types, $config->getFieldname( 'publishedid' ), $javascript, 'value','text', $model->getField( $config->getFieldname( 'publishedid' ) ) ); 
			$this->assignRef('filterLists', $filterLists );
			
		// set search
			$searchterm = $model->getField( $config->getFieldname( 'search' ) );
			$search = "<input type='text' name='".$config->getFieldname( 'search' )."' value='$searchterm' class='text_area' onchange='submitForm(\"".HelpDeskRequest::getVar( 'task' )."\")' />";
			$this->assignRef( 'search' , $search );
			
		// pagenavigation
			$this->assignRef( 'order' , $model->getOrder() );
			$this->assignRef( 'order_dir' , $model->getOrderDir() );
			$pagination = $model->getPagination();    
			$this->assignRef( 'pagination' , $pagination);
		
		// form
			$formAction = "index2.php";
			$this->assignRef( 'formAction' , $formAction );
			$validate = josSpoofValue();
			$formValidate = "<input type='hidden' name='{$validate}' value='1' />"; 
			$this->assignRef( 'formValidate' , $formValidate );
			$this->assignRef( 'formOption' , HelpDeskRequest::getVar( 'option' ) );
			$this->assignRef( 'formController' , $this->_name );
			$this->assignRef( 'formId' , $model->_id );
		
		parent::display($tpl);
		
	}

	
	/**
	 * 
	 * @param $tpl
	 * @return unknown_type
	 */	
	function _form($tpl)
	{
		$mainframe	= HelpDeskConfig::getApplication();
		$user 		= HelpDeskConfig::getUser();
		$database 	= HelpDeskConfig::getDBO();
		$model 		= $this->getModel();
		$app 		= HelpDeskConfig::getApplication();
		
		// check config		
			$config = &HelpDeskConfig::getInstance();
			$this->assignRef( 'config', $config );

        // Get data from the model
			$row = $model->getTable('tool');
			$row->load( (int) $model->getId() );
			
		// load the plugin
			$import = HelpDeskHelperPlugin::importPlugin( strtolower( 'HelpDesk' ), $row->element );
			
		// Assign variables
			$pagetitle  = HelpDeskText::_( 'Tool' );
			$this->assignRef('pagetitle', $pagetitle );
			$this->assignRef('user', $user );
	        $this->assignRef('row', $row);
	        
		// form
			$formAction = "index2.php";
			$this->assignRef( 'formAction' , $formAction );
			$validate = josSpoofValue();
			$formValidate = "<input type='hidden' name='{$validate}' value='1' />"; 
			$this->assignRef( 'formValidate' , $formValidate );
			$this->assignRef( 'formOption' , HelpDeskRequest::getVar( 'option' ) );
			$this->assignRef( 'formController' , $this->_name );
			$this->assignRef( 'formId' , $model->_id );

		// can the plugin be run?
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$before 	= $dispatcher->trigger( 'onBeforeDisplayToolHelpDesk', array( $row ) );
			if (in_array(false, $before, true)) {
				HelpDeskError::raiseError( 500, $row->getError() );
				return false;
			}
		
		// OK, so display the plugin
			parent::display($tpl);

		// and some post-display stuff, if necessary
			$dispatcher->trigger( 'onAfterDisplayToolHelpDesk', array( $row ) );

	}
	
}